package com.kbt.util.pay;

import com.kbt.activity.GoodForPayActivity;

import java.net.URLEncoder;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhgk
 * @package: com.kbt.util.pay
 * @TODO: <请描述这个类是干什么的>
 * @data: 2015/9/3 12:53
 * @version: V1.0
 */
public class PayUtil {
    // 商户PID
    public static final String PARTNER = "2088701649700796";
    // 商户收款账号
    public static final String SELLER = "5184778@qq.com";
    // 商户私钥，pkcs8格式
    public static final String RSA_PRIVATE = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOe+yg8dxUuq02F/dQI43bOTPk3auFjakcxNFTb3Ga9utU7HFKslKrgATk88htSy39w88AdtwWHItAizCZGMHSUZq9zRQd932xLVnEL68dZDCdbXA75lqhA4EdArmH3ll+//IR1cYPNRBTfsfqRDl7EDR7v58T9lQoT1i1fpMeFZAgMBAAECgYEAxqIcdNpymknARqhFtXLibCH/3CFlsxscrr0/Os6PYHQGyZAP4iXIONBiTr41Xyg2goJkxilIALVTwsUapXswGlQcHpYkp00TZLXuT2pQQum2+89vrJEFeOXmw2Ckik9FSXhm9ybOUEUoCQaphF8cuVgPlYMPnDdi4/ODkxDigaUCQQDzwN7uqQQUDRVw8UUtA7ZBIeQP/Zm2gnulWazXef+O58OlO3qBdAuO/3IQTRoP3ClFI2vm7va66nh23hRDFzyfAkEA82N4obCy8v0lS8C4I08dDxdHUaSHh6o9bP/pEfS1F2uaw3POAVUngi6Kx7ABkw26pQ77ViWxdQXAKp+LQs0nBwJAWrMPwPp/G/7qVYZRWTiiXoX/2LtisPjIjXRazisFHuW6dQc2xGirzF/cz5gA9JnVeXZ+tgWBJXgNrx1fqL9CvQJBAKgYiEUr87vMyIh0Nvjz9/7nNfOWLaCuHDR+3qrvFm9xevcIsTvMYzgPCvx1e/DjZ4f+O+o4qEJJUXperGDbrdsCQQC4T+SuduS+vpiZ8if7gaLFO79F5fEV7uzODQh5UrwzEwYEb7x5P8t+XjGD/JxmbWP7oF5OhRDEL3ojGnWzTX4i";

    // 支付宝公钥
    public static final String RSA_PUBLIC = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";



    public static final int SDK_PAY_FLAG = 1;

    public static final int SDK_CHECK_FLAG = 2;
    private static final String ALGORITHM = "RSA";
    private static final String SIGN_ALGORITHMS = "SHA1WithRSA";
    private static final String DEFAULT_CHARSET = "UTF-8";

    //获得订单信息的方法
    public static String getNewOrderInfo(String subject, String body, String fee) {

        StringBuilder sb = new StringBuilder();
        sb.append("service=\"mobile.securitypay.pay\"");
        sb.append("&partner=\"").append(PARTNER).append("\"");// 合作身份者id
        sb.append("&out_trade_no=\"").append(GoodForPayActivity.pre_id).append("\"");//这个是订单编号
        sb.append("&subject=\"").append(subject).append("\"");//这个应该是商品名称
        sb.append("&body=\"").append(body).append("\"");//这个应该是商品的描述，具体你可以参考demo
        sb.append("&total_fee=\"").append(fee).append("\"");//这个是要付款的金额，到时候你调用的时候改下就行了
        // 网址需要做URL编码
        //服务器异步通知页面,完成交易后通知商家服务器的页面，以post的形式将商品订单信息发送到指定页面，手机客户端不需要可以先放在这不管
        sb.append("&notify_url=\"").append(URLEncoder.encode("http://notify.msp.hk/notify.htm")).append("\"");
        sb.append("&_input_charset=\"UTF-8\"");
        sb.append("&payment_type=\"1\"");
        sb.append("&seller_id=\"").append(SELLER).append("\"");

        // 如果show_url值为空，可不传
        // sb.append("\"&show_url=\"");
        sb.append("&it_b_pay=\"15m\"");

        return new String(sb);
    }

    public static String getOrderNo() {
        Date date = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddmmhhssSSS");
        String order = fm.format(date.getTime());
        return order;
    }

    public static String sign(String content) {
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
                    Base64.decode(RSA_PRIVATE));
            KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);

            java.security.Signature signature = java.security.Signature
                    .getInstance(SIGN_ALGORITHMS);

            signature.initSign(priKey);
            signature.update(content.getBytes(DEFAULT_CHARSET));

            byte[] signed = signature.sign();

            return Base64.encode(signed);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String getSignType() {
        return "sign_type=\"RSA\"";
    }
}
